﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Management;

namespace 数据库复制工具toSQLServer
{
    public partial class RegInfo_Form : Form
    {
        public RegInfo_Form()
        {
            InitializeComponent();
        }

        ///// <summary>
        ///// 获取CPU——ID
        ///// </summary>
        ///// <returns></returns>
        //[DllImport("CPU_ID.dll", EntryPoint = "cpu_id")]
        //private static extern string get_cpu_id();

        private string ZhuanH(string text)
        {
            string cpuid = text;
            string Id = text;
            for (int i = 0; i < (cpuid.Length / 4) - 1; i++)
            {
                Id = Id.Insert(4 + (4 * i + i), "-");
            }
            return Id;
        }

        private string ZhuanH(string text, bool zhuce)
        {
            string temp = ZhuanH(text);
            int count = temp.LastIndexOf('-');
            string temp2 = "";
            for (int i = 0; i < count; i++)
            {
                temp2 += temp[i];
            }
            return temp2;
        }

        private void RegInfo_Form_Load(object sender, EventArgs e)
        {

            textBox1.Text = ZhuanH(GetCpuID().ToUpper());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                MessageBox.Show("请填写正确的注册码！");
                return;
            }
            ClassJiami jiami = new ClassJiami();
            string HostId = ZhuanH(jiami.Encrypto(textBox1.Text).ToUpper(), true);
            if (HostId == textBox2.Text.Trim())
            {
                RegistryKey_Operate reg = new RegistryKey_Operate("NetWorkScan");
                reg.WTRegedit("RegisteredValue", textBox2.Text.Trim());
                MessageBox.Show("注册成功！");
                DialogResult = DialogResult.Yes;
                this.Close();
            }
            else
            {
                MessageBox.Show("注册失败！请与厂家取得联系！");
                DialogResult = DialogResult.No;
                this.Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public String GetCpuID()
        {
            try
            {
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                String strCpuID = null;
                foreach (ManagementObject mo in moc)
                {
                    strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                    break;
                }
                return strCpuID;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return "";
            }
        }
    }
}
